﻿package src.index_copa{
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.external.*;
	import flash.utils.*;
	import flash.net.*;
	import fl.controls.*;
	import caurina.transitions.*;
	import src.*;
	public class Main extends Sprite {
		public var stageW:Number;
		public var stageH:Number;
		private var one:Boolean = true;
		private var loader:Loader = new Loader();
		private var loading:TextField = new TextField();
		private var menuList:Array;
		private var menuNum:Number = 1;
		private var colors:Array;
		private var letters:Array;
		//
		private var timeout:int;
		public function Main() {
			stage.scaleMode = "noScale";
			stage.align = "topLeft";
			stage.addEventListener(Event.RESIZE, resizeHd);
			colors = [0xFF3533, 0xFDD200, 0x62C2F8, 0xAAC800];
			letters = ["C", "o", "p", "a", "C", "A", "S", "T"];
			init();
		}
		private function init():void {
			logo.addEventListener(MouseEvent.MOUSE_MOVE, moveHd);
			menu_init();
			layout();
		}
		private function moveHd(e:Event):void {
			if (Math.random() > 0.5) {
				var letter:Letter = new Letter(letters[Math.floor(Math.random()*letters.length)], colors[Math.floor(Math.random()*colors.length)], 12, 10, 1);
				addChild(letter);
			}
		}
		private function resizeHd(e:Event):void {
			one = false;
			layout();
		}
		//开始布局绘图
		private function layout():void {
			stageW = stage.stageWidth;
			stageH = stage.stageHeight;
			//
			if (stageW > 1000) {
				logo.x = (stageW - 1000)/2 + 10;
				menu.x = (stageW - 1000)/2 + 620;
				login.x = (stageW - 1000)/2 + 780;
				footer.x = logo.x;
				loader.x = (stageW - 1000)/2 + 390;
				//
				bg.width = stageW;
			}
			if (stageH > 600) {
				//logo.y = (stageH - 600)/2;
				//menu.y = logo.y;
				//login.y = logo.y;
				//loader.y = (stageH - 600)/2 + 120;
				footer.y = stageH - footer.height - 10;
			}
		}
		//取得用户路径
		private function gUU():String {
			var systemUrl:String = this.root.loaderInfo.loaderURL.split("?")[0];
			var url:String;
			if (systemUrl.lastIndexOf("\\") != -1) {
				url = systemUrl.slice(0,systemUrl.lastIndexOf("\\") + 1);
			} else {
				url = systemUrl.slice(0,systemUrl.lastIndexOf("/") + 1);
			}
			return url;
		}
		//menu//////////////////////////////////////////////////////
		private function menu_init():void {
			menuList = [[menu.menu1,"tab_index.swf",1],[menu.menu2,"tab_adnetworks.swf",2],[menu.menu3,"tab_adnetworks.swf",3],[menu.menu4,"tab_aboutus.swf",4],[menu.menu5,"tab_aboutus.swf",5]];
			for (var i:Number = 0; i<menuList.length; i++) {
				var themenu:MovieClip = menuList[i][0] as MovieClip;
				themenu.buttonMode = true;
				themenu.tabEnabled = false;
				themenu.num = menuList[i][2];
				themenu.tablink = menuList[i][1];
				themenu.addEventListener(MouseEvent.MOUSE_OVER, overHd);
				themenu.addEventListener(MouseEvent.MOUSE_OUT, outHd);
				themenu.addEventListener(MouseEvent.CLICK, clickHd);
			}
			//
			loader.x = 390;
			loader.y = 150;
			addChild(loader);
			loading.selectable = false;
			//loading.background = true;
			loading.mouseEnabled = false;
			loading.autoSize = "center";
			addChild(loading);
			loadTab("tab_index.swf");
		}
		private function overHd(e:Event):void {
			e.currentTarget.alpha = 0.6;
		}
		private function outHd(e:Event):void {
			e.currentTarget.alpha = 1;
		}
		private function clickHd(e:Event):void {
			var themenu:MovieClip = e.currentTarget as MovieClip;
			menuNum = themenu.num;
			loadTab(themenu.tablink);
		}
		//tab////////////////////////////////////////////////////////
		private function loadTab(url:String):void {
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			loader.contentLoaderInfo.sharedEvents.addEventListener("tabchange", changeHd);
			var request:URLRequest = new URLRequest(gUU() + url);
			loader.load(request);
		}
		private function progressHandler(e:ProgressEvent):void {
			//trace("progressHandler: bytesLoaded=" + e.bytesLoaded + " bytesTotal=" + e.bytesTotal);
			loading.x = loader.x + 270 - loading.width/2;
			loading.y = loader.y + 80;
			var per:Number = Math.round(e.bytesLoaded/e.bytesTotal * 100);
			if (per < 100) {
				loading.text = "LOADING... " + per + "%";
				loading.setTextFormat(new TextFormat("Verdana", 9, 0x666666, false));
			}
		}
		private function completeHandler(e:Event):void {
			//O.o("completeHandler: " + event);
			loading.text = "";
			if (menuNum == 5) {
				var mc:MovieClip = loader.content as MovieClip;
				mc.tabnum = 4;
				mc.init();
			}
		}
		private function ioErrorHandler(e:IOErrorEvent):void {
			loading.text = "";
			//O.o("ioErrorHandler: " + e);
		}
		//=============================================================
		public function changeHd(e:Event):void {
			//clearTimeout(timeout);
			var mc:MovieClip = loader.content as MovieClip;
			var num:int = mc.tabnum;
			//
			if (num) {
				//O.o("111");
				var l:int = logo.circles.numChildren - 1;
				gogo(num, l, 0);
			}
		}
		private function gogo(num:Number, l:int, i:int):void {
			var cc:Circle = logo.circles.getChildAt(i) as Circle;
			//trace(cc.name);
			if (cc) {
				cc.goto(num);
			}
			i++;
			if (i < l) {
				timeout = setTimeout(gogo, 50, num, l, i);
			}
		}
	}
}